package com.yxcz.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.yxcz.model.UpdateUserForm;
import com.yxcz.model.UpdateUserInfoForm;
import com.yxcz.model.User;
import com.yxcz.service.LoginService;
import com.yxcz.service.PositionService;
import com.yxcz.service.UserService;
import com.yxcz.util.ImageHelper;

@Controller
@RequestMapping("/userManager")
public class UserManagerController {

	@Resource
	private PositionService positionService;
	@Resource
	private UserService userService;
	@Resource
	private LoginService loginService;

	/**
	 * 登陆页面控制
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView loginPage(ModelAndView modelAndView) {
		modelAndView.setViewName("/Manager/User");
		modelAndView.addObject("users", userService.list());
		modelAndView.addObject("positions", positionService.list());
		return modelAndView;
	}

	/**
	 * 获取用户信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getUser")
	@ResponseBody
	public User getUser(Integer id) {

		if (id == null || id == 0)
			return null;

		return userService.find(id);

	}

	/**
	 * 修改用户信息
	 * @param user
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView update(@Validated UpdateUserForm form,BindingResult bindingResult, ModelAndView modelAndView) {
        
		if (bindingResult.hasErrors())
			modelAndView.setViewName("redirect:/Common/500Page");
		else {
			userService.updateUser(form);
			modelAndView.addObject("msg", "修改");
			modelAndView.setViewName("Common/success");
		}

		return modelAndView;

	}
	
	
	@RequestMapping("/delete")
	public ModelAndView delete(Integer id, ModelAndView modelAndView) {

		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		userService.delete(id);
		modelAndView.addObject("msg", "删除");
		modelAndView.setViewName("Common/success");

		return modelAndView;

	}
	

	/**
	 * 更新用户头像
	 * 
	 * @param photourl
	 * @param request
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/updatePhoto")
	public ModelAndView updateUserInfo(String photourl,Integer id,HttpServletRequest request, ModelAndView modelAndView, int x,
			int y, int w, int h) {
		modelAndView.addObject("msg", "修改");
		if (photourl == null || photourl == "") {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		String path = request.getSession().getServletContext().getRealPath("") + photourl;
		try {
			String type = photourl.substring(photourl.lastIndexOf(".") + 1, photourl.length());
			ImageHelper.cutImage(path, path, x, y, w, h, type);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		userService.updateUserPhoto(id,photourl);
		modelAndView.setViewName("Common/success");
		return modelAndView;

	}
	
	/**
	 * 通过管理员修改密码
	 * @param password
	 * @param repassword
	 * @param id
	 * @param session
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "/doChangePassword")
	public ModelAndView doChangePassword(String password, String repassword,Integer id,HttpSession session,ModelAndView modelAndView) {

		if (id==null||password == null || password == "" || !password.equals(repassword) ) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		
		userService.updatePassword(userService.find(id),password);

		modelAndView.setViewName("Common/success");
		return modelAndView;

	}
	
	
	@RequestMapping(value = "/doAddUser")
	public ModelAndView doAddUser(String accountName, String password,ModelAndView modelAndView) {

		if (accountName==null||accountName==""||password==null||password==""||loginService.checkAccountNameIsExits(accountName)!=false) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
        try {
			userService.add(accountName,password);
		} catch (Exception e) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		modelAndView.setViewName("Common/success");
		return modelAndView;

	}
	
	
	
	

}
